package g11.action;

import g11.dao.DAOException;
import g11.dao.DrugDAOImpl;
import g11.mis.DrugTypeEnum;
import g11.model.Drug;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class DrugAction extends ActionSupport implements SessionAware,
		RequestAware {

	private Map<String, Object> session;
	private Map<String, Object> request;
	private String actionMode;
	private Drug retrievedDrug;
	private String selectedDrugId;

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	@Override
	public void setRequest(Map request) {
		this.request = request;
	}
	
	public String getActionMode() {
		return actionMode;
	}

	public void setActionMode(String actionMode) {
		this.actionMode = actionMode;
	}

	public Drug getRetrievedDrug() {
		return retrievedDrug;
	}

	public void setRetrievedDrug(Drug retrievedDrug) {
		this.retrievedDrug = retrievedDrug;
	}

	public String getSelectedDrugId() {
		return selectedDrugId;
	}

	public void setSelectedDrugId(String selectedDrugId) {
		this.selectedDrugId = selectedDrugId;
	}

	public String initCreateDrugPage() {
		session.put("triggeredAction", "initCreateDrugPage");
		this.retrievedDrug = new Drug();
		this.actionMode = "create";
		session.put("actionResult","");
		return SUCCESS;
	}
	
	public String initUpdateDrugPage(){
		
		Integer drugId = Integer.valueOf(this.selectedDrugId);
		DrugDAOImpl dDao = new DrugDAOImpl();
		try {
			this.retrievedDrug = dDao.getDrugById(drugId);
		} catch (DAOException e) {
			e.printStackTrace();
		}
		session.put("actionResult","");
		this.actionMode = "update";
		return SUCCESS;
	}
	
	public String initViewDrugPage(){
		session.put("actionResult","");
		return SUCCESS;
	}
	
	public String createDrug() {
		session.put("triggeredAction", "createDrug");
		DrugDAOImpl dDao = new DrugDAOImpl();
		
		try {
			dDao.createDrug(retrievedDrug);
		} catch (DAOException e) {
			e.printStackTrace();
			return ERROR;
		}
		session.put("actionResult","Drug "+retrievedDrug.getDrugName()+" successfully created !!");
		return SUCCESS;
	}
	
	public String updateDrug(){
		session.put("triggeredAction", "updateDrug");
		DrugDAOImpl dDao = new DrugDAOImpl();
		
		try {
			dDao.updateDrug(retrievedDrug);
		} catch (DAOException e) {
			e.printStackTrace();
			return ERROR;
		}
		session.put("actionResult","Drug "+retrievedDrug.getDrugName()+" successfully updated !!");
		return SUCCESS;
	}
	
	public Map<String,String> getDrugTypeMap(){
		Map<String,String> typeMap = new HashMap<String,String>();
		for(DrugTypeEnum dte: DrugTypeEnum.values()){
			typeMap.put(String.valueOf(dte.index()), dte.toString().toLowerCase());
		}
		return typeMap;
	}

}
